-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #1552
base: master
Are you sure you want to change the base?
Solution #1552
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job!!!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job, but review my comments and fix them for all the cases 🥹
src/App.tsx
Outdated
(async () => { | ||
try { | ||
const data = await todoService.getTodos(); | ||
|
||
setTodos(data); | ||
} catch (err) { | ||
setErrorMessage(Errors.UnableToLoad); | ||
} | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks better, what do u think?
(async () => { | |
try { | |
const data = await todoService.getTodos(); | |
setTodos(data); | |
} catch (err) { | |
setErrorMessage(Errors.UnableToLoad); | |
} | |
})(); | |
const getAllTodos = async () => { | |
try { | |
const data = await todoService.getTodos(); | |
setTodos(data); | |
} catch (err) { | |
setErrorMessage(Errors.UnableToLoad); | |
} | |
} | |
getAllTodos(); |
src/App.tsx
Outdated
const onAddTodo = async (todoTitle: string) => { | ||
setTempTodo({ | ||
id: 0, | ||
title: todoTitle, | ||
completed: false, | ||
userId: todoService.USER_ID, | ||
}); | ||
try { | ||
const newTodo = await todoService.addTodo({ | ||
title: todoTitle, | ||
completed: false, | ||
}); | ||
|
||
setTodos(prev => [...prev, newTodo]); | ||
} catch (err) { | ||
setErrorMessage(Errors.UnableToAdd); | ||
inputAddRef?.current?.focus(); | ||
throw err; | ||
} finally { | ||
setTempTodo(null); | ||
} | ||
}; | ||
|
||
const onRemoveTodo = async (todoId: number) => { | ||
setLoadingTodoIds(prev => [...prev, todoId]); | ||
try { | ||
await todoService.deleteTodo(todoId); | ||
|
||
setTodos(prev => prev.filter(todo => todo.id !== todoId)); | ||
} catch (err) { | ||
setErrorMessage(Errors.UnableToDelete); | ||
inputAddRef?.current?.focus(); | ||
throw err; | ||
} finally { | ||
setLoadingTodoIds(prev => prev.filter(id => id !== todoId)); | ||
} | ||
}; | ||
|
||
const onUpdateTodo = async (todoToUpdate: Todo) => { | ||
setLoadingTodoIds(prev => [...prev, todoToUpdate.id]); | ||
try { | ||
const updatedTodo = await todoService.updateTodo(todoToUpdate); | ||
|
||
setTodos(prev => | ||
prev.map(todo => (todo.id === updatedTodo.id ? updatedTodo : todo)), | ||
); | ||
} catch (err) { | ||
setErrorMessage(Errors.UnableToUpdate); | ||
throw err; | ||
} finally { | ||
setLoadingTodoIds(prev => prev.filter(id => id !== todoToUpdate.id)); | ||
} | ||
}; | ||
|
||
const onToggleAll = async () => { | ||
if (todosActiveNumber > 0) { | ||
todos | ||
.filter(todo => !todo.completed) | ||
.forEach(item => onUpdateTodo({ ...item, completed: true })); | ||
} else { | ||
todos.forEach(todo => onUpdateTodo({ ...todo, completed: false })); | ||
} | ||
}; | ||
|
||
const onClearCompleted = async () => { | ||
const completedTodo = todos.filter(todo => todo.completed); | ||
|
||
completedTodo.forEach(todo => onRemoveTodo(todo.id)); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need useCallback
for these cases?
src/App.tsx
Outdated
const filteredTodos = todos.filter(todo => { | ||
switch (filteredField) { | ||
case FilterType.Active: | ||
return !todo.completed; | ||
case FilterType.Completed: | ||
return todo.completed; | ||
default: | ||
return todos; | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- move this function to separate component and use
useMemo
for variable - or just use useMemo
1 is better option
src/App.tsx
Outdated
{(todos.length > 0 || tempTodo) && ( | ||
<> | ||
<TodoList | ||
todos={filteredTodos} | ||
onRemoveTodo={onRemoveTodo} | ||
onUpdateTodo={onUpdateTodo} | ||
loadingTodoIds={loadingTodoIds} | ||
tempTodo={tempTodo} | ||
/> | ||
<Footer | ||
todos={todos} | ||
filteredField={filteredField} | ||
setFilteredField={setFilteredField} | ||
activeTodo={todosActiveNumber} | ||
onClearCompleted={onClearCompleted} | ||
/> | ||
</> | ||
)} | ||
</div> | ||
<ErrorNotification |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- use
!!
to convert value to boolean type - don't forget about indents
{(todos.length > 0 || tempTodo) && ( | |
<> | |
<TodoList | |
todos={filteredTodos} | |
onRemoveTodo={onRemoveTodo} | |
onUpdateTodo={onUpdateTodo} | |
loadingTodoIds={loadingTodoIds} | |
tempTodo={tempTodo} | |
/> | |
<Footer | |
todos={todos} | |
filteredField={filteredField} | |
setFilteredField={setFilteredField} | |
activeTodo={todosActiveNumber} | |
onClearCompleted={onClearCompleted} | |
/> | |
</> | |
)} | |
</div> | |
<ErrorNotification | |
{(!!todos.length || tempTodo) && ( | |
<> | |
<TodoList | |
todos={filteredTodos} | |
onRemoveTodo={onRemoveTodo} | |
onUpdateTodo={onUpdateTodo} | |
loadingTodoIds={loadingTodoIds} | |
tempTodo={tempTodo} | |
/> | |
<Footer | |
todos={todos} | |
filteredField={filteredField} | |
setFilteredField={setFilteredField} | |
activeTodo={todosActiveNumber} | |
onClearCompleted={onClearCompleted} | |
/> | |
</> | |
)} | |
</div> | |
<ErrorNotification |
useEffect(() => { | ||
inputRef?.current?.focus(); | ||
}, [todosLength, inputRef]); | ||
|
||
useEffect(() => { | ||
if (!isInputDisabled) { | ||
inputRef?.current?.focus(); | ||
} | ||
}, [isInputDisabled, inputRef]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks strange
useEffect(() => { | |
inputRef?.current?.focus(); | |
}, [todosLength, inputRef]); | |
useEffect(() => { | |
if (!isInputDisabled) { | |
inputRef?.current?.focus(); | |
} | |
}, [isInputDisabled, inputRef]); | |
useEffect(() => { | |
inputRef?.current?.focus(); | |
}, [todosLength, inputRef]); | |
useEffect(() => { | |
if (!isInputDisabled) { | |
inputRef?.current?.focus(); | |
} | |
}, [isInputDisabled, inputRef]); |
src/components/Header/Header.tsx
Outdated
|
||
return ( | ||
<header className="todoapp__header"> | ||
{todosLength !== 0 && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use this syntax for all the cases(more readable)
{todosLength !== 0 && ( | |
{!!todosLength && ( |
src/types/rest.tsx
Outdated
// const updateTodo = (itemId, updatedTitle) => { | ||
// const unmodifiedTodo = todos.find(todo => todo.id === itemId); | ||
|
||
// if (!unmodifiedTodo) { | ||
// endChaning(itemId); | ||
|
||
// return; | ||
// } | ||
|
||
// if (updatedTitle === unmodifiedTodo.title) { | ||
// return; | ||
// } | ||
|
||
// const updatedTodo = { | ||
// ...unmodifiedTodo, | ||
// title: updatedTitle !== null ? updatedTitle.trim() : unmodifiedTodo.title, | ||
// completed: | ||
// updatedTitle === null | ||
// ? !unmodifiedTodo.completed | ||
// : unmodifiedTodo.completed, | ||
// }; | ||
|
||
// if (updatedTitle !== null && updatedTodo.title === '') { | ||
// return deleteTodo(itemId); | ||
// } | ||
|
||
// setTodos(prev => | ||
// prev.map(todo => (todo.id === itemId ? updatedTodo : todo)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove unnecessary comments from ur project
@@ -0,0 +1,46 @@ | |||
/* eslint-disable @typescript-eslint/no-explicit-any */ | |||
const BASE_URL = 'https://mate.academy/students-api'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move all the CONSTANTS to separate directory - constants
If I do, the tests fail Co-authored-by: VolodymyrKirichenko <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a masterpiece 🔥
DEMO LINK